package uk.ac.lkl.lpp.model.xml;




import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import uk.ac.lkl.common.util.ds.NotifyingList;

import uk.ac.lkl.lpp.model.PedagogyPlanModel;

import uk.ac.lkl.lpp.model.ldo.LearningOutcome;
//import uk.ac.lkl.lpp.model.ldo.TeachingMethod;



public class LearningOutcomeSetProcessor extends ModelElementProcessor {

    public LearningOutcomeSetProcessor() {
        super("LearningOutcomeSet");
    }


    public void convertDataToElements(PedagogyPlanModel plan,
                                      Document document,
                                      Element pedagogyPlanElement) {
        // should be done in base class
        Element learningOutcomeSetElement = document.createElement("LearningOutcomeSet");
        pedagogyPlanElement.appendChild(learningOutcomeSetElement);

        NotifyingList<LearningOutcome> outcomes = (NotifyingList<LearningOutcome>) plan.getList(LearningOutcome.class);
        for (int i = 0; i < outcomes.size(); i++) {
            LearningOutcome learningOutcome = outcomes.get(i);
            Element learningOutcomeElement = document.createElement("LearningOutcome");

            learningOutcomeElement.setAttribute("index", Integer.toString(i));

            String title = learningOutcome.getTitle();
            learningOutcomeElement.setAttribute("title", title);

            String description = learningOutcome.getDescription();
            learningOutcomeElement.setTextContent(description);

            learningOutcomeSetElement.appendChild(learningOutcomeElement);
        }
    }


    public void convertElementToData(PedagogyPlanModel plan, Element element) {
        NotifyingList<LearningOutcome> outcomes = new NotifyingList<LearningOutcome>(
                                                                                     LearningOutcome.class);
        NodeList outcomeNodes = element.getElementsByTagName("LearningOutcome");
        for (int i = 0; i < outcomeNodes.getLength(); i++) {
            Element outcomeElement = (Element) outcomeNodes.item(i);
            String title = outcomeElement.getAttribute("title");
            String description = outcomeElement.getTextContent();
            LearningOutcome outcome = new LearningOutcome(title, description);
            outcomes.add(outcome);
        }
        plan.addList(LearningOutcome.class, outcomes);
    }

}
